<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filters && !loading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          v-for="brand in filters?.brands"
          @click="
            filters.selectedBrandId = brand.id;
            updateSelectedFilters();
          "
          :key="brand.id"
          :class="{ active: filters.selectedBrandId === brand.id }"
          >{{ brand.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="item in filters?.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a
          href="javascript:"
          v-for="goods in item.properties"
          :key="goods.id"
          @click="
            item.selectedFilterName = goods.name;
            updateSelectedFilters();
          "
          :class="{ active: item.selectedFilterName === goods.name }"
          >{{ goods.name }}</a
        >
      </div>
    </div>
  </div>
  <div class="sub-filter" v-else>
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { ref } from "vue";
import { onBeforeRouteUpdate, useRoute } from "vue-router";
import { getSubCategoryList } from "@/api/categroy";

export default {
  name: "SubFilter",
  emits: ["onFilterChanged"],
  setup(props, { emit }) {
    // 获取路由信息
    const route = useRoute();
    const { getData, filters, loading, updateSelectedFilters } =
      getSubFilter(emit);
    getData(route.params.id);
    return { filters, loading, updateSelectedFilters };
  },
};

function getSubFilter(emit) {
  // 储存数据
  const filters = ref();
  // 是否正在加载数据
  const loading = ref(false);
  // 整合筛选数据
  const selectedFilters = {
    brandId: null,
    attrs: [],
  };
  // 汇总整合数据
  const updateSelectedFilters = () => {
    // 汇总临时储存的brandId
    selectedFilters.brandId = filters.value.selectedBrandId;
    // 清除原有数据
    selectedFilters.attrs = [];
    // 汇总选择的条件
    filters.value.saleProperties.forEach((item) => {
      // 如果存在 且 不是全部
      if (item.selectedFilterName && item.selectedFilterName !== "全部") {
        // 将用户选择的筛选条件 储存
        selectedFilters.attrs.push({
          groupName: item.name,
          propertyName: item.selectedFilterName,
        });
        console.log(selectedFilters);
      }
    });
    // 将用户选择的筛选条件传递到父组件
    emit("onFilterChanged", selectedFilters);
  };
  // 获取数据
  const getData = (id) => {
    loading.value = true;

    getSubCategoryList(id).then((data) => {
      // 给每一项添加一个全部选项
      data.result.brands.unshift({ name: "全部", id: null });
      data.result.saleProperties.forEach((item) => {
        item.properties.unshift({ name: "全部", id: null });
        // 设置默认选中的筛选条件的名字
        item.selectedFilterName = "全部";
      });
      // 设置默认选中的品牌Id
      data.result.selectedBrandId = null;
      // 获取数据
      filters.value = data.result;
      // 更新加载状态
      loading.value = false;
    });
  };
  onBeforeRouteUpdate((to) => getData(to.params.id));

  return { getData, filters, loading, updateSelectedFilters };
}
</script>
<style scoped lang="less">
.xtx-skeleton {
  padding: 10px 0;
}
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
